package com.zb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.CarinfoService;
import com.zb.Service.CarorderService;
import com.zb.Service.TaskService;
import com.zb.entity.Carinfo;
import com.zb.entity.Carorder;
import com.zb.entity.Department;
import com.zb.entity.Task;
import com.zb.entity.Tasktype;
import com.zb.entity.User;

@Scope("prototype")
@Component
@RequestMapping("/car")
public class CarController {
	
	@Resource
	private TaskService taskService;
	@Resource
	private CarinfoService CarinfoService;
	@Resource
	private CarorderService CarorderService;
	//显示用车信息
	@RequestMapping(params = "op=carinfo")
	@ResponseBody
	public List<Carinfo> showCar(){
		List<Carinfo> list=CarinfoService.findCarinfo();	
		return list;	
	}
	//修改用车信息
	@RequestMapping(params = "op=updcarinfo")
	@ResponseBody
	public Map<String,Object> updateCar(Integer id,String carno,String carkind,String tip){
		Map<String,Object> map = new HashMap<String, Object>();
		Carinfo carinfo=new Carinfo();
		carinfo.setId(id);
		carinfo.setCarno(carno);
		carinfo.setCarkind(carkind);
		carinfo.setTip(tip);
		CarinfoService.updateCarinfo(carinfo);
		map.put("success", true);
		map.put("message", "保存成功！");
		return map;	
	}
	@RequestMapping(params = "op=addcarinfo")
	@ResponseBody
	public Map<String,Object> addCar(String carnos,String carkinds,String tips){
		Map<String,Object> map = new HashMap<String, Object>();
		Carinfo carinfo=new Carinfo();
		carinfo.setCarno(carnos);
		carinfo.setCarkind(carkinds);
		carinfo.setTip(tips);
		CarinfoService.saveCarinfo(carinfo);
		map.put("success", true);
		map.put("message", "保存成功！");
		return map;	
	}
	
	@RequestMapping(params = "op=delcarinfo")
	public String deleteCar(Integer id, ModelMap modelmap){
		List<Carorder> list=CarorderService.findCarorder(id);
		if(list.size()>0){
			modelmap.put("e", "有外键引用，无法删除");
		}else{
			CarinfoService.deleteCarinfo(id);
		}
		return "carmanager";
	}
	
	//添加申请
	@RequestMapping(params = "op=addcarorder")
	@ResponseBody
	public Map<String,Object> addCarOrder(Integer carinfoz,String driver,String otheruser,
			Integer department,String destination,Integer usercar,String starttime,String endtime,Integer length,String content,Integer nextuser){
		Map<String,Object> map = new HashMap<String, Object>();
		Carinfo carinfo=new Carinfo();
		Carorder carorder=new Carorder();
		Department depart=new Department();
		User user=new User();
		carinfo.setId(carinfoz);
		carorder.setCarinfo(carinfo);
		carorder.setDriver(driver);
		carorder.setOtheruser(otheruser);
		depart.setId(department);
		carorder.setDepartment(depart);
		carorder.setDestination(destination);
		user.setId(usercar);
		carorder.setUsercar(user);
		carorder.setStarttime(starttime);
		carorder.setEndtime(endtime);
		carorder.setLength(length);
		carorder.setContent(content);
		CarorderService.addCarorder(carorder);
		
		Task task=new Task();
		Tasktype Tasktype=new Tasktype();
		User User=new User();
		User.setId(usercar);
		task.setUserByStartuser(User);
		Tasktype.setId(3);
		task.setTasktype(Tasktype);
		task.setObjectid(carorder.getId());
		task.setStarttime(starttime);
		User User1=new User();
		User1.setId(nextuser);
		task.setUserBySenduser(User1);
		
		taskService.savecaroTask(task); 
		map.put("success", true);
		map.put("message", "保存成功！");
		return map;	
	} 
}
